TKey
TValue
CSharpTest.Net
KeyValueSerializer<TKey,TValue> Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Serialization Namespace : KeyValueSerializer<TKey,TValue> Class

Glossary Item Box

Implements ISerializer of KeyValuePair<TKey, TValue>

Syntax

Visual Basic (Declaration) 
Public NotInheritable Class KeyValueSerializer
    (Of TKey,TValue) 
   Implements CSharpTest.Net.Serialization.ISerializer(Of KeyValuePair(Of TKey,TValue)) 

Type Parameters

TKey
TValue

Example

Library/Library.Test/TestPrimitiveSerializer.cs

C#Copy Code
ISerializer<KeyValuePair<int, Guid>> ser = new KeyValueSerializer<int, Guid>(PrimitiveSerializer.Int32, PrimitiveSerializer.Guid);
Dictionary<int, Guid> values = new Dictionary<int, Guid>
{
    { -1, Guid.NewGuid() },
    { 0, Guid.NewGuid() },
    { 1, Guid.NewGuid() },
    { int.MaxValue, Guid.NewGuid() },
};

foreach (KeyValuePair<int, Guid> value in values)
{
    using (MemoryStream ms = new MemoryStream())
    {
        ser.WriteTo(value, ms);
        // add random bytes, every value should know it's length and not rely on EOF.
        byte[] bytes = new byte[256 - ms.Position];
        _random.NextBytes(bytes);
        ms.Write(bytes, 0, bytes.Length);
        // seek begin and read.
        ms.Position = 0;
        Assert.AreEqual(value, ser.ReadFrom(ms));
    }
}
VB.NETCopy Code
Dim ser As ISerializer(Of KeyValuePair(Of Integer, Guid)) = New KeyValueSerializer(Of Integer, Guid)(PrimitiveSerializer.Int32, PrimitiveSerializer.Guid)
Dim values As New Dictionary(Of Integer, Guid)() From { _
    {-1, Guid.NewGuid()}, _
    {0, Guid.NewGuid()}, _
    {1, Guid.NewGuid()}, _
    {Integer.MaxValue, Guid.NewGuid()} _
}

For Each value As KeyValuePair(Of Integer, Guid) In values
    Using ms As New MemoryStream()
        ser.WriteTo(value, ms)
        ' add random bytes, every value should know it's length and not rely on EOF.
        Dim bytes As Byte() = New Byte(256 - ms.Position) {}
        _random.NextBytes(bytes)
        ms.Write(bytes, 0, bytes.Length)
        ' seek begin and read.
        ms.Position = 0
        Assert.AreEqual(value, ser.ReadFrom(ms))
    End Using
Next

Inheritance Hierarchy

System.Object
   CSharpTest.Net.Serialization.KeyValueSerializer<TKey,TValue>

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys